# Pivot wider to create a matrix-like table
ktemp_wide <- ktemp %>%
pivot_wider(names_from = module, values_from = n, values_fill = 0)
# Set 'subcategory' as rownames
ktemp_mat <- ktemp_wide %>%
column_to_rownames('subcategory') %>%
as.matrix()
ktemp_wide %>%
column_to_rownames('subcategory')
ktemp_wide
ktemp_wide %>%
as.matrix()
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(pheatmap)
kegg = all_kegg_results
# Drop rows where subcategory is NA
kegg <- kegg %>%
filter(!is.na(subcategory))
# Filter significant terms (p.adjust < 0.05) and count
ktemp <- kegg %>%
filter(p.adjust < 0.05) %>%
count(subcategory, module)
a_present <- a[a %in% unique(ktemp$subcategory)]
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(pheatmap)
kegg = all_kegg_results
# Drop rows where subcategory is NA
kegg <- kegg %>%
filter(!is.na(subcategory))
# Filter significant terms (p.adjust < 0.05) and count
ktemp <- kegg %>%
filter(p.adjust < 0.05) %>%
count(subcategory, module)
library(readr)
library(dplyr)
library(tidyr)
# Pivot wider to create a matrix-like table
ktemp_wide <- ktemp %>%
pivot_wider(names_from = module, values_from = n, values_fill = 0)
# Set 'subcategory' as rownames
ktemp_mat <- ktemp_wide %>%
as.matrix()
# Plot heatmap
pheatmap(
ktemp_mat,
color = viridis::viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
display_numbers = FALSE,
border_color = "white",
gaps_row = NULL,
gaps_col = NULL,
main = "Significant KEGG Terms",
labels_row = NULL,
labels_col = NULL,
fontsize = 10
)
ktemp_mat
# Plot heatmap
pheatmap(
ktemp_mat,
color = viridis::viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
display_numbers = FALSE,
border_color = "white",
gaps_row = NULL,
gaps_col = NULL,
main = "Significant KEGG Terms",
labels_row = NULL,
labels_col = NULL,
fontsize = 10
)
# Plot heatmap
pheatmap(
ktemp_mat,
color = viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
border_color = "white",
fontsize = 10,
main = "Significant KEGG Terms"
)
# Plot heatmap
library(viridis)
pheatmap(
ktemp_mat,
color = viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
border_color = "white",
fontsize = 10,
main = "Significant KEGG Terms"
)
library(viridis)
# Plot heatmap
pheatmap(
ktemp_mat,
color = viridis::viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
display_numbers = FALSE,
border_color = "white",
gaps_row = NULL,
gaps_col = NULL,
main = "Significant KEGG Terms",
labels_row = NULL,
labels_col = NULL,
fontsize = 10
)
ktemp_mat
library(viridis)
# Important: Make sure all columns are numeric (they are currently character!)
ktemp_mat <- ktemp_mat %>%
mutate(across(everything(), as.numeric)) %>%
as.matrix()
library(viridis)
# Important: Make sure all columns are numeric (they are currently character!)
ktemp_mat <- ktemp_wide %>%
mutate(across(everything(), as.numeric)) %>%
as.matrix()
# Now plot
pheatmap(
ktemp_mat,
color = viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
border_color = "white",
fontsize = 10,
main = "Significant KEGG Terms"
)
ktemp_mat
library(readr)
library(dplyr)
library(tidyr)
# Pivot wider
ktemp_wide <- ktemp %>%
pivot_wider(names_from = module, values_from = n, values_fill = 0)
# Set rownames and remove 'subcategory' column
ktemp_mat <- ktemp_wide %>%
column_to_rownames('subcategory')
# Set 'subcategory' as rownames
ktemp_mat <- ktemp_wide %>%
as.matrix()
library(viridis)
# Important: Make sure all columns are numeric (they are currently character!)
ktemp_mat <- ktemp_wide %>%
mutate(across(everything(), as.numeric)) %>%
as.matrix()
# Now plot
pheatmap(
ktemp_mat,
color = viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
border_color = "white",
fontsize = 10,
main = "Significant KEGG Terms"
)
library(readr)
library(dplyr)
library(tidyr)
# Pivot wider
ktemp_wide <- ktemp %>%
pivot_wider(names_from = module, values_from = n, values_fill = 0)
# Set rownames and remove 'subcategory' column
ktemp_mat <- ktemp_wide %>%
column_to_rownames('subcategory')
library(viridis)
# Important: Make sure all columns are numeric (they are currently character!)
ktemp_mat <- ktemp_mat %>%
mutate(across(everything(), as.numeric)) %>%
as.matrix()
# Now plot
pheatmap(
ktemp_mat,
color = viridis(100),
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 0,
border_color = "white",
fontsize = 10,
main = "Significant KEGG Terms"
)
library(viridis)
# Important: Make sure all columns are numeric (they are currently character!)
ktemp_mat <- ktemp_mat %>%
mutate(across(everything(), as.numeric)) %>%
as.matrix()
# --- Masking zeros: make them NA ---
ktemp_mat_masked <- ktemp_mat
ktemp_mat_masked[ktemp_mat_masked == 0] <- NA
# --- Sort rows based on increasing order of first module (column "1") ---
# (if you want to sort by a specific module index)
module1 <- "1"  # choose the module you want to sort by
if (module1 %in% colnames(ktemp_mat_masked)) {
ktemp_mat_masked <- ktemp_mat_masked[order(ktemp_mat_masked[, module1], decreasing = TRUE, na.last = TRUE), ]
}
# --- Plot heatmap ---
pheatmap(
ktemp_mat_masked,
color = viridis(100, na.color = "white"),  # White color for NA (masked)
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 45,       # Slight tilt to x labels
border_color = "grey90",
fontsize_row = 9,
fontsize_col = 10,
legend_breaks = c(1, 3, 5, 7, 10),
main = "Significant KEGG Terms",
cellwidth = 20,       # Force cells to be square
cellheight = 20       # Force cells to be square
)
ktemp_mat_masked
# --- Masking zeros: make them NA ---
ktemp_mat_masked <- ktemp_mat
ktemp_mat_masked[ktemp_mat_masked == 0] <- NA
# --- Sort rows based on increasing order of first module (column "1") ---
# (if you want to sort by a specific module index)
module1 <- "1"  # choose the module you want to sort by
if (module1 %in% colnames(ktemp_mat_masked)) {
ktemp_mat_masked <- ktemp_mat_masked[order(ktemp_mat_masked[, module1], decreasing = TRUE, na.last = TRUE), ]
}
# --- Plot heatmap ---
pheatmap(
ktemp_mat_masked,
color = viridis(100),  # White color for NA (masked)
cluster_rows = FALSE,
cluster_cols = FALSE,
angle_col = 45,       # Slight tilt to x labels
border_color = "grey90",
fontsize_row = 9,
fontsize_col = 10,
legend_breaks = c(1, 3, 5, 7, 10),
main = "Significant KEGG Terms",
cellwidth = 20,       # Force cells to be square
cellheight = 20       # Force cells to be square
)
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on module 1 counts ---
sorting_order <- ktemp_long %>%
filter(module == 1) %>%
arrange(desc(n)) %>%
pull(subcategory)
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# --- Plot ---
ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(
option = "D",
na.value = "white",
name = "Significant terms",
limits = c(0, max(ktemp$n, na.rm = TRUE)),
breaks = c(1, 3, 5, 7, 10)  # Choose nice breaks
) +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 1  # <-- makes tiles square
) +
labs(title = "Significant KEGG Terms by Module")
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on module 1 counts ---
sorting_order <- ktemp_long %>%
#filter(module == 1) %>%
arrange(desc(n)) %>%
pull(subcategory)
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on module 1 counts ---
sorting_order <- ktemp_long %>%
#filter(module == 1) %>%
#arrange(desc(n)) %>%
pull(subcategory)
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# --- Plot ---
ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(
option = "D",
na.value = "white",
name = "Significant terms",
limits = c(0, max(ktemp$n, na.rm = TRUE)),
breaks = c(1, 3, 5, 7, 10)  # Choose nice breaks
) +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 1  # <-- makes tiles square
) +
labs(title = "Significant KEGG Terms by Module")
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(option = "D", na.value = "white", name = "Significant terms") +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 1
) +
labs(title = "Significant KEGG Terms by Module")
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# Create heatmap
p <- ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(option = "D", na.value = "white", name = "Significant terms") +
theme_minimal(base_size = 14) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 1,   # <-- force squares
plot.title = element_text(hjust = 0.5)
) +
labs(title = "Significant KEGG Terms by Module")
# Show
print(p)
# Save it
ggsave(
filename = "kegg_heatmap.pdf",
plot = p,
width = 8,     # in inches
height = 8,    # square
units = "in",
dpi = 300
)
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# Create heatmap
p <- ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(option = "D", na.value = "white", name = "Significant terms") +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 1,   # <-- force squares
plot.title = element_text(hjust = 0.5),
axis.text = element_text(size = 8) # smaller axis text for fitting
) +
labs(title = "Significant KEGG Terms by Module")
# Show the plot
print(p)
# Save it, set exact square dimensions (e.g., 7x7 inches)
ggsave(
filename = "kegg_heatmap_square.pdf",
plot = p,
width = 7,    # Adjusted to 7x7 for better aspect ratio
height = 7,   # Ensuring it's square
units = "in",
dpi = 300
)
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# Create heatmap
p <- ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(option = "D", na.value = "white", name = "Significant terms") +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = .5,   # <-- force squares
plot.title = element_text(hjust = 0.5),
axis.text = element_text(size = 8) # smaller axis text for fitting
) +
labs(title = "Significant KEGG Terms by Module")
# Show the plot
print(p)
# Save it, set exact square dimensions (e.g., 7x7 inches)
ggsave(
filename = "kegg_heatmap_square.pdf",
plot = p,
width = 7,    # Adjusted to 7x7 for better aspect ratio
height = 7,   # Ensuring it's square
units = "in",
dpi = 300
)
# --- Pivot longer for ggplot ---
ktemp_long <- ktemp %>%
mutate(module = as.factor(module))  # Make module categorical
# --- Sort subcategories based on NAME alphabetically ---
sorting_order <- ktemp_long %>%
distinct(subcategory) %>%     # get unique subcategories
arrange(subcategory) %>%      # sort alphabetically
pull(subcategory)
# Then use it to reorder
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
ktemp_long <- ktemp_long %>%
mutate(subcategory = factor(subcategory, levels = sorting_order))
# Create heatmap
p <- ggplot(ktemp_long, aes(x = module, y = subcategory, fill = n)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_viridis(option = "D", na.value = "white", name = "Significant terms") +
theme_minimal(base_size = 12) +
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
axis.text.y = element_text(hjust = 1),
panel.grid = element_blank(),
axis.title = element_blank(),
aspect.ratio = 2,   # <-- force squares
plot.title = element_text(hjust = 0.5),
axis.text = element_text(size = 8) # smaller axis text for fitting
) +
labs(title = "Significant KEGG Terms by Module")
# Show the plot
print(p)
# Save it, set exact square dimensions (e.g., 7x7 inches)
ggsave(
filename = "kegg_heatmap_square.pdf",
plot = p,
width = 7,    # Adjusted to 7x7 for better aspect ratio
height = 7,   # Ensuring it's square
units = "in",
dpi = 300
)
